package com.douyuehan.doubao.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.douyuehan.doubao.common.exception.ApiAsserts;
import com.douyuehan.doubao.jwt.JwtUtil;
import com.douyuehan.doubao.project.mapper.BmsTopicMapper;
import com.douyuehan.doubao.project.mapper.BmsUserMapper;
import com.douyuehan.doubao.project.model.dto.LoginDTO;
import com.douyuehan.doubao.project.model.dto.RegisterDTO;
import com.douyuehan.doubao.project.model.entity.BmsUser;
import com.douyuehan.doubao.project.model.vo.ProfileVO;
import com.douyuehan.doubao.project.service.BmsUserService;
import com.douyuehan.doubao.utils.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;

@Transactional(rollbackFor = Exception.class)
@Slf4j
@Service
public class BmsUserServiceImpl extends ServiceImpl<BmsUserMapper,BmsUser> implements BmsUserService {

    @Autowired
    private BmsTopicMapper bmsTopicMapper;

    @Override
    public BmsUser userRegister(RegisterDTO registerDTO) {
        //如果输入数据库已经存在的用户名和邮箱则返回
        LambdaQueryWrapper<BmsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BmsUser::getUsername, registerDTO.getName()).or().eq(BmsUser::getEmail, registerDTO.getEmail());
        BmsUser bmsUser = baseMapper.selectOne(wrapper);
        if(!ObjectUtils.isEmpty(bmsUser)){
            ApiAsserts.fail("账号或邮箱已存在：");
        }

        BmsUser addUser = new BmsUser()
                .setUsername(registerDTO.getName())
                .setAlias(registerDTO.getName())
                .setPassword(MD5Utils.getPwd(registerDTO.getPass()))//把加密后的密码存入数据库
                .setEmail(registerDTO.getEmail())
                .setCreateTime(new Date())
                .setStatus(true);
        baseMapper.insert(addUser);

        return addUser;
    }

    @Override
    public BmsUser getUserByUsername(String username) {
        //select * from bms_user where user_name=username
        return baseMapper.selectOne(new LambdaQueryWrapper<BmsUser>().eq(BmsUser::getUsername, username));
    }
    @Override
    public String executeLogin(LoginDTO dto) {
        String token = null;
        try {
            BmsUser user = getUserByUsername(dto.getUsername());//根据用户名获取用户信息
            String encodePwd = MD5Utils.getPwd(dto.getPassword());//把登录密码加密
            if(!encodePwd.equals(user.getPassword()))
            {
                throw new Exception("密码错误");
            }
            token = JwtUtil.generateToken(String.valueOf(user.getUsername()));//根据用户名生成token
        } catch (Exception e) {
            log.warn("用户不存在or密码验证失败=======>{}", dto.getUsername());
        }
        return token;
    }

    @Override
    public ProfileVO getUserProfile(String id) {
        ProfileVO profile = new ProfileVO();
        BmsUser user = baseMapper.selectById(id);
        //只把user对象中和profile对象相同的属性的值copy给profile对象
        BeanUtils.copyProperties(user, profile);

        return profile;
    }



}
